<?php
namespace app\controller\admin;

use app\controller\AdminController;
use app\model\PickupModel;
use app\model\SettingModel;
use think\facade\Request;

/**
 * 自提点管理控制器
 */
class Pickup extends AdminController
{
    /**
     * 获取自提点列表
     * @return \think\response\Json
     */
    public function index()
    {
        $filter = [];
        $filter['name'] = Request::param('name');
        $filter['status'] = Request::param('status');
        $filter['address'] = Request::param('address');
        
        $result = PickupModel::get_list($filter);
        
        return json($result);
    }

    /**
     * 获取单个自提点信息
     * @return \think\response\Json
     */
    public function single()
    {
        $id = Request::param('id');
        
        if (empty($id)) {
            return json([
                'code' => 1,
                'message' => '缺少参数'
            ]);
        }
        
        $info = PickupModel::single($id);
        
        return json($info);
    }

    /**
     * 编辑/新增自提点
     * @return \think\response\Json
     */
    public function edit()
    {
        $pickup_id = Request::param('id');
        
        // 验证必填字段
        $name = Request::param('name');
        if (empty($name)) {
            return json([
                'code' => 1,
                'message' => '请输入名称'
            ]);
        }
        
        $phone = Request::param('phone');
        if (empty($phone)) {
            return json([
                'code' => 1,
                'message' => '请输入电话'
            ]);
        }
        
        $province = Request::param('province');
        if (empty($province)) {
            return json([
                'code' => 1,
                'message' => '请输入省份'
            ]);
        }
        
        $city = Request::param('city');
        if (empty($city)) {
            return json([
                'code' => 1,
                'message' => '请输入城市'
            ]);
        }
        
        $area = Request::param('area');
        if (empty($area)) {
            return json([
                'code' => 1,
                'message' => '请输入区、县'
            ]);
        }
        
        $street = Request::param('street');
        if (empty($street)) {
            return json([
                'code' => 1,
                'message' => '请输入街道'
            ]);
        }
        
        $detail = Request::param('detail');
        if (empty($detail)) {
            return json([
                'code' => 1,
                'message' => '请输入详细地址'
            ]);
        }
        
        // 组装数据
        $data = [
            'name' => $name,
            'phone' => $phone,
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'street' => $street,
            'detail' => $detail,
            'latitude' => Request::param('latitude', ''),
            'longitude' => Request::param('longitude', ''),
            'status' => Request::param('status', 1),
        ];
        
        // 执行操作
        if (!empty($pickup_id)) {
            $ret = PickupModel::edit($pickup_id, $data);
            $message = '修改成功';
        } else {
            $ret = PickupModel::add($data);
            $pickup_id = $ret;
            $message = '添加成功';
        }
        
        return json([
            'code' => 0,
            'message' => $message,
            'id' => $pickup_id,
            'data' => $ret
        ]);
    }

    /**
     * 删除自提点
     * @return \think\response\Json
     */
    public function remove()
    {
        $id = Request::param('id');
        
        if (empty($id)) {
            return json([
                'code' => 1,
                'message' => '请选中你要删除的自提点'
            ]);
        }
        
        $ret = PickupModel::remove($id);
        
        return json([
            'code' => 0,
            'message' => '删除成功',
            'data' => $ret
        ]);
    }

    /**
     * 坐标转地址（调用腾讯地图API）
     * @return \think\response\Json|string
     */
    public function latlng_to_addr()
    {
        $lat = Request::param('lat');
        $lng = Request::param('lng');
        
        if (empty($lat) || empty($lng)) {
            return json([
                'code' => 1,
                'message' => '缺少座标参数'
            ]);
        }
        
        // 获取腾讯地图KEY
        $setting_item = SettingModel::get('config', 'latlng_to_addr_key');
        if (empty($setting_item['value'])) {
            return json([
                'code' => 1,
                'message' => '请先在后台设置腾讯位置服务KEY'
            ]);
        }
        
        $key = $setting_item['value'];
        $url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' . $lat . ',' . $lng
            . '&key=' . $key . '&get_poi=0';
        
        // 使用CURL调用接口
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        
        $response = curl_exec($ch);
        curl_close($ch);
        
        return $response;
    }
}